import 'package:get/get.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_inventory_tab_model.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/subs/pro_inventory/state.dart';
import 'package:jinke/utils/httputils/api.dart';
import 'package:jinke/utils/httputils/resultdata.dart';

import '../../model/pro_inventory_model.dart';

class ProInventoryController extends GetxController {
  ProInventoryController();
  final state = ProInventoryState();
  _initData() {
    getTabs();
  }

  void _update() {
    update(["pro_inventory"]);
  }

  void onTabTap(int index) {
    state.tabIndex = index;
    getList();
    _update();
  }

  void getTabs() {
    if (state.mod == null) {
      return;
    }
    var id = state.mod?.id ?? '';
    ProListApi.postInventoryTab(id: id).then((value) {
      ResultData res = value;
      if (res.isSuccess) {
        List? data = res.data;
        state.tabs =
            data?.map((e) => ProInventoryTabModel.fromJson(e)).toList();
        _update();
        getList();
      }
    });
  }

  void getList() {
    if (state.tabs == null) {
      return;
    }
    var tab = state.tabs![state.tabIndex];
    ProListApi.postInventoryList(
            busId: state.mod?.id ?? '', personType: tab.personType ?? '')
        .then((value) {
      ResultData res = value;
      if (res.isSuccess) {
        List? data = res.data['content'];
        state.list = data?.map((e) => ProInventoryModel.fromJson(e)).toList();
        _update();
      }
    });
  }
  // @override
  // void onInit() {
  //   super.onInit();
  // }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
